import static org.junit.Assert.*;
import gui.AcademicFrame;
import gui.ServerGui;

import java.util.ArrayList;

import junit.framework.TestCase;

import logic.PDFGenerator;

import org.junit.Assert;
import org.junit.Test;

import Entities.RequestEntity;


public class FrozenTest extends TestCase{

	ServerGui server;
	AcademicFrame client;
	RequestEntity request1;
	RequestEntity request2;
	RequestEntity request3;
	RequestEntity request4;
	RequestEntity request5;
	RequestEntity request6;
	RequestEntity request7;
	RequestEntity request8;
	public int fileLength=0;
	public byte[] bytes;
	double arr[];
	
	public void setUp()
	{
		server = new ServerGui();
		server.fakeServer(5555, "mysql://localhost/test", "root", "Braude");
		server.myLog.setVisible(false);
		client = new AcademicFrame("Localhost",5555);
		initialize();
	}
	
	public void initialize(){
		request1= new RequestEntity("","","","","Chen","Parodi","01/06/2013","","",null,"103","Moodle");
		request2= new RequestEntity("","","","","Chen","Parodi","02/06/2013","","",null,"103","Moodle");
		request3= new RequestEntity("","","","","Chen","Parodi","15/06/2013","","",null,"103","Moodle");
		request4= new RequestEntity("","","","","Chen","Parodi","30/06/2013","","",null,"103","Moodle");
		request5= new RequestEntity("","","","","Chen","Parodi","01/07/2013","","",null,"103","Moodle");
		request6= new RequestEntity("","","","","Chen","Parodi","02/07/2013","","",null,"103","Moodle");
		request7= new RequestEntity("","","","","Chen","Parodi","15/08/2013","","",null,"103","Moodle");
		if(makeNewRecords(request1) && makeNewRecords(request2) && makeNewRecords(request3) && makeNewRecords(request4)
				 && makeNewRecords(request5) && makeNewRecords(request6) && makeNewRecords(request7)){
			if(setFrozen(request1) && setFrozen(request2) && setFrozen(request3) && 
					setFrozen(request4) && setFrozen(request5) && setFrozen(request6) && setFrozen(request7))
			getActivity();
			
		}
	}
	
	public boolean makeNewRecords(RequestEntity request){
		ArrayList<Object> message = new ArrayList<Object>();
		ArrayList<Object> answer = new ArrayList<Object>();
		message.add("Request");
		message.add(request);
		message.add(bytes);
		message.add(fileLength);	

		client.myclient.sendMessageToServer(message);
		while (client.answer==null);
		answer = client.answer;
		client.answer=null;
		
		if(answer.get(0).equals("returnedRequest"))
			return true;
		else
			return false;
	}		
	public boolean getActivity() {
		
		ArrayList<Object> message = new ArrayList<Object>();
		ArrayList<Object> answer = new ArrayList<Object>();
		message.add("getActivityReport");
		message.add("06/2013");
		message.add("");
		client.myclient.sendMessageToServer(message);
		while (client.answer==null);
		answer = client.answer;
		client.answer=null;
		
		if(answer.get(0).equals("ActivityReport")){
			arr=(double[]) answer.get(9);
			return true;
		}
		else
			return false;
	}
	public boolean getTheNewRecords(){
		ArrayList<Object> message = new ArrayList<Object>();
		ArrayList<Object> answer = new ArrayList<Object>();
		message.add("getReq");
		message.add("1");	
		client.myclient.sendMessageToServer(message);
		while (client.answer==null);
		answer = client.answer;
		client.answer=null;
		
		if(answer.get(0).equals("ridlist")){
			int rid1=Integer.parseInt(answer.get(2).toString());
			int rid2=Integer.parseInt(answer.get(3).toString());
			int rid3=Integer.parseInt(answer.get(4).toString());
			int rid4=Integer.parseInt(answer.get(5).toString());
			int rid5=Integer.parseInt(answer.get(6).toString());
			int rid6=Integer.parseInt(answer.get(7).toString());
			int rid7=Integer.parseInt(answer.get(8).toString());
			request1.setRid(rid1);
			request2.setRid(rid2);
			request3.setRid(rid3);
			request4.setRid(rid4);
			request5.setRid(rid5);
			request6.setRid(rid6);
			request7.setRid(rid7);
			return true;
		}
		else
			return false;
	}
	public boolean setFrozen(RequestEntity request){
		ArrayList<Object> message = new ArrayList<Object>();
		ArrayList<Object> answer = new ArrayList<Object>();
		message.add("complete");
		message.add(Integer.toString(request.getRid()));
		message.add("0");
		client.myclient.sendMessageToServer(message);
		while (client.answer==null);
		answer = client.answer;
		client.answer=null;
		
		if(answer.get(0).equals("changeStatus")){
			return true;
		}
		else
			return false;
	}
	
	public void testMedian()
	{
		double expected=4.0;
		System.out.println(expected);
		//double expected=3.263;
		double notexpected=3.0;
		double result=PDFGenerator.median(arr);
		System.out.println(result);
		System.out.println("resultttttttttttttttttt= "+result);
		Assert.assertTrue(expected==result);
		Assert.assertFalse(notexpected==result);
	}
	public void testgetSD()
	{
		double expected=0;
		System.out.println(expected);
		//double expected=0.617;
		double notexpected=3.0;
		double result=PDFGenerator.standardDeviation(arr);
		System.out.println(result);
		System.out.println("resultttttttttttttttttt= "+result);
		Assert.assertTrue(expected==result);
		Assert.assertFalse(notexpected==result);
	}
		
		
		
		

}



